/*
 *  Copyright 2010 dmakariev.
 * 
 */
package org.deltaset.faces.upload;

import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.io.IOUtils;

/**
 *
 * @author dmakariev
 */
final class CurrentFileImpl implements CurrentFile {
    private static final Logger logger = Logger.getLogger(CurrentFileImpl.class.getName());
    private final String contentType;
    private final String name; 
    private final byte[] content;
    private final int fileSize;

    private CurrentFileImpl(String contentType, String name, int fileSize, byte[] content) {
        this.contentType = contentType;
        this.name = name;
        this.fileSize = fileSize;
        this.content = content;         
    }


    public static CurrentFile fromFileItemStream(FileItemStream fileItemStream) {
        try {
            final String contentType = fileItemStream.getContentType();
            final String name = fileItemStream.getName();
            final InputStream is = fileItemStream.openStream();
            final byte[] content = IOUtils.toByteArray(is);
           
            logger.log(Level.INFO, " CurrentFile.fromFileItemStream name={0} content.length={1}", new Object[]{name, content.length});
            return new CurrentFileImpl(contentType, name,content.length, content);
        } catch (IOException ex) {
            logger.log(Level.SEVERE, ex.toString(),ex);
            throw new RuntimeException(ex);
        }
    }

    /**
     * @return the name
     */
    @Override
    public String getName() {
        return name;
    }

    /**
     * @return the contentType
     */
    @Override
    public String getContentType() {
        return contentType;
    }

    @Override
    public int getFileSize() {
        return fileSize;
    }        

    /**
     * @return the content
     */
    @Override
    public byte[] getContent() {
        return content;
    }

    @Override
    public String toString() {
        return "CurrentFile{" + "name=" + name +", contentType=" + contentType + ", fileSize=" + fileSize + '}';
    }
           

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final CurrentFileImpl other = (CurrentFileImpl) obj;
        if ((this.contentType == null) ? (other.contentType != null) : !this.contentType.equals(other.contentType)) {
            return false;
        }
        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
            return false;
        }
        if (this.fileSize != other.fileSize) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 29 * hash + (this.contentType != null ? this.contentType.hashCode() : 0);
        hash = 29 * hash + (this.name != null ? this.name.hashCode() : 0);
        hash = 29 * hash + this.fileSize;
        return hash;
    }
    
}
